home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Utilities / KPerfMon / Source / AppDelegate.m < prev    next >
Text File  |  1992-11-17  |  11KB  |  327 lines

  1. /*
  2.   Copyright 1991 Scott Hess.  Permission to use, copy, modify, and
  3.   distribute this software and its documentation for any purpose
  4.   and without fee is hereby granted, provided that this copyright
  5.   notice appear in all copies.  The copyright notice need not appear
  6.   on binary-only distributions - just in source code.
  7.   
  8.   Scott Hess makes no representations about the suitability of this
  9.   software for any purpose.  It is provided "as is" without express
  10.   or implied warranty.
  11. */
  12.  
  13. /* Generated by Interface Builder */
  14.  
  15. /******************************************************************************************************************************
  16.  *    As its name implies, this object is the application delegate.  It handles setting up the program and cleaning up        *
  17.  *    when the program terminates.                                                                                            *
  18.  ******************************************************************************************************************************/
  19.  
  20. //#import <mach.h>
  21. #import <mach/mach.h>        /* for 3.0 compatibility -- drue */
  22. #import <stdlib.h>
  23. //#import <defaults.h>
  24. #import <defaults/defaults.h>    /* for 3.0 compatibility -- drue */
  25. #import <appkit/nextstd.h>
  26. #import <appkit/Application.h>
  27. #import <appkit/Button.h>
  28. #import <appkit/NXImage.h>
  29. #import "AppDelegate.h"
  30. #import "Monitor.h"
  31.  
  32. #define MINPOLLINTERVAL 0.1
  33. #define MINFLIPINTERVAL 0.1
  34.  
  35. float pollInterval = 1.0;
  36.  
  37. @implementation AppDelegate
  38.  
  39. - appWillInit:sender
  40. {
  41. struct task_basic_info     tbi;
  42. unsigned int         ic = TASK_BASIC_INFO_COUNT;
  43.  
  44.     if( task_info(task_self(),TASK_BASIC_INFO,(task_info_t)&tbi,&ic) != KERN_SUCCESS )
  45.         return nil;
  46.     task_priority(task_self(),tbi.base_priority-4,TRUE);
  47.  
  48.     return self;
  49. }
  50.  
  51. - appDidInit:sender
  52. {
  53. Window    *iconWindow;
  54. id     iconView;
  55. NXImage *recessed;
  56. NXRect    monitorRect;
  57. NXDefaultsVector defs =
  58.     {
  59.     {"PollInterval","1.0"},
  60.     {"FlipInterval","0.0"},
  61.     {"DisplayInfo", "CDMN"},
  62.     {"InfoType","0"},
  63.     {NULL,NULL}
  64.     };
  65.  
  66.     /**************************************************************************************************************************
  67.      *    Instantiate the NXImages needed to do the drawing                                                                   *
  68.      **************************************************************************************************************************/
  69.     recessed = [NXImage findImageNamed:"recessed.tiff"];
  70.  
  71.     /**************************************************************************************************************************
  72.      *    Get the application icon's view and put our own views there.                                                        *
  73.      **************************************************************************************************************************/
  74.     iconWindow = [NXApp appIcon];
  75.     iconView = [iconWindow contentView];
  76.     [iconView addSubview:monitor];
  77.     [iconView lockFocus];
  78.     [monitor getFrame:&monitorRect];
  79.     [recessed composite:NX_COPY toPoint:&(monitorRect.origin)];
  80.     [iconView unlockFocus];
  81.  
  82.     [iconWindow flushWindow];
  83.  
  84.     [recessed free];
  85.  
  86.     /**************************************************************************************************************************
  87.      *    Register the defaults with the system.                                                                              *
  88.      **************************************************************************************************************************/
  89.     NXRegisterDefaults([NXApp appName],defs);
  90.     if ( NXGetDefaultValue([NXApp appName],"PollInterval") )
  91.         {
  92.         pollInterval = atof(NXGetDefaultValue([NXApp appName],"PollInterval"));
  93.         pollInterval = MAX(pollInterval,MINPOLLINTERVAL);
  94.     [pollSlider setFloatValue:pollInterval];
  95.         [pollText setFloatValue:pollInterval];
  96.         }
  97.     
  98.     if ( NXGetDefaultValue([NXApp appName],"FlipInterval") )
  99.         {
  100.         flipInterval = atof(NXGetDefaultValue([NXApp appName],"FlipInterval"));
  101.         if ( flipInterval > MINFLIPINTERVAL )
  102.         {
  103.         [flipSlider setFloatValue:flipInterval];
  104.             [flipText setFloatValue:flipInterval];
  105.         }
  106.         else
  107.             {
  108.         [flipSlider setFloatValue:0.0];
  109.         [flipText setStringValue:"OFF"];
  110.         }
  111.         }
  112.     
  113.     displayCpu = displayDisk = displayMemory = displayNetwork = TRUE;
  114.     if ( NXGetDefaultValue([NXApp appName],"DisplayInfo") )
  115.         {
  116.     strcpy( displayInfo, NXGetDefaultValue([NXApp appName],"DisplayInfo"));
  117.       //  a lowercase value means the matching option is turned off
  118.     if ( strchr(displayInfo, 'c') ) displayCpu = FALSE;
  119.     if ( strchr(displayInfo, 'd') ) displayDisk = FALSE;
  120.     if ( strchr(displayInfo, 'm') ) displayMemory = FALSE;
  121.     if ( strchr(displayInfo, 'n') ) displayNetwork = FALSE;
  122.         }
  123.     if ( (displayCpu + displayDisk + displayMemory + displayNetwork) < 1) displayCpu = TRUE;
  124.     if ( displayCpu ) [cpuSwitch setState:1];
  125.     if ( displayDisk ) [diskSwitch setState:1];
  126.     if ( displayMemory ) [memorySwitch setState:1];
  127.     if ( displayNetwork ) [networkSwitch setState:1];
  128.     
  129.     if ( NXGetDefaultValue([NXApp appName],"InfoType") )
  130.         {
  131.         tag = atoi(NXGetDefaultValue([NXApp appName],"InfoType"));
  132.         switch ( tag )
  133.             {
  134.             case CPU:
  135.                 break;
  136.             case MEMORY:
  137.                 break;
  138.             case DISK:
  139.                 break;
  140.             case NETWORK:
  141.                 break;
  142.             default:
  143.                 tag = CPU;
  144.                 }
  145.         }
  146.     [monitor flip:self];
  147.  
  148.     /**************************************************************************************************************************
  149.      *    Register the timed entry with the window server.                                                                    *
  150.      **************************************************************************************************************************/
  151.     stepEntry = DPSAddTimedEntry(pollInterval,(void *)_step,monitor,NX_MODALRESPTHRESHOLD);
  152.     if ( flipInterval > MINFLIPINTERVAL )
  153.         flipEntry = DPSAddTimedEntry(flipInterval*60,(void *)_flip,self,NX_MODALRESPTHRESHOLD);
  154.     return self;    
  155. }
  156.  
  157. - appWillTerminate:sender
  158. {
  159.     if ( stepEntry )
  160.         {
  161.         DPSRemoveTimedEntry(stepEntry);
  162.         stepEntry = (DPSTimedEntry)0;
  163.         }
  164.     if ( flipEntry )
  165.         {
  166.         DPSRemoveTimedEntry(flipEntry);
  167.         flipEntry = (DPSTimedEntry)0;
  168.         }
  169.     return self;
  170. }
  171.  
  172. - info:sender
  173. {
  174.     if ( !infoPanel )
  175.         [NXApp loadNibSection:"InfoPanel.nib" owner:self];
  176.     [infoPanel makeKeyAndOrderFront:self];
  177.     return self;
  178. }
  179.  
  180. - flipMonitor
  181. {
  182.     tag++;
  183.     if ( tag > TYPE_MAX )
  184.         tag = 0;
  185.  
  186.     //  ordering of the following checks is important (they work based on the
  187.     //  specific values and order-in-values of the tags in question).
  188.     if ( tag == CPU && ! displayCpu ) tag++;        // checking tag == 0
  189.     if ( tag == DISK && ! displayDisk) tag++;        // checking tag == 1
  190.     if ( tag == MEMORY && ! displayMemory ) tag++;    // checking tag == 2
  191.     if ( tag == NETWORK && ! displayNetwork) tag++;    // checking tag == 3
  192.     if ( tag > TYPE_MAX )
  193.         tag = 0;
  194.     [monitor flip:self];
  195.     return self;
  196. }
  197.  
  198. - setPollInterval:sender
  199. {
  200. char intervalString[6];
  201.  
  202.     pollInterval = MAX([sender floatValue],MINPOLLINTERVAL);
  203.     sprintf(intervalString,"%4.1f",pollInterval);
  204.     NXWriteDefault([NXApp appName],"PollInterval",intervalString);
  205.     if ( stepEntry )
  206.         {
  207.         DPSRemoveTimedEntry(stepEntry);
  208.         stepEntry = (DPSTimedEntry)0;
  209.         }
  210.     stepEntry = DPSAddTimedEntry(pollInterval,(void *)_step,monitor,NX_MODALRESPTHRESHOLD);
  211.     [pollText setStringValue:intervalString];
  212.     return self;
  213. }
  214.  
  215. - setFlipInterval:sender
  216. {
  217. char intervalString[6];
  218.  
  219.     flipInterval = [sender floatValue];
  220.     if ( flipEntry )
  221.         {
  222.         DPSRemoveTimedEntry(flipEntry);
  223.         flipEntry = (DPSTimedEntry)0;
  224.         }
  225.     if ( flipInterval > MINFLIPINTERVAL )
  226.         {
  227.         sprintf(intervalString,"%4.1f",flipInterval);
  228.         flipEntry = DPSAddTimedEntry(flipInterval*60,(void *)_flip,self,NX_MODALRESPTHRESHOLD);
  229.         }
  230.     else
  231.         strcpy(intervalString,"OFF");
  232.     [flipText setStringValue:intervalString];
  233.     NXWriteDefault([NXApp appName],"FlipInterval",intervalString);
  234.     return self;
  235. }
  236.  
  237. - setDisplayCpu:sender
  238. {
  239.     displayCpu = [cpuSwitch state];
  240.     [self saveDisplayInfo];
  241.     if ( displayCpu ) { tag = CPU ; [monitor flip:self]; }
  242.     else if ( tag == CPU ) { [self flipMonitor]; }
  243.     return self;
  244. }
  245.  
  246. - setDisplayDisk:sender
  247. {
  248.     displayDisk = [diskSwitch state];
  249.     [self saveDisplayInfo];
  250.     if ( displayDisk ) { tag = DISK ; [monitor flip:self]; }
  251.     else if ( tag == DISK ) { [self flipMonitor]; }
  252.     return self;
  253. }
  254.  
  255. - setDisplayMemory:sender
  256. {
  257.     displayMemory = [memorySwitch state];
  258.     [self saveDisplayInfo];
  259.     if ( displayMemory ) { tag = MEMORY ; [monitor flip:self]; }
  260.     else if ( tag == MEMORY ) { [self flipMonitor]; }
  261.     return self;
  262. }
  263.  
  264. - setDisplayNetwork:sender
  265. {
  266.     displayNetwork = [networkSwitch state];
  267.     [self saveDisplayInfo];
  268.     if ( displayNetwork ) { tag = NETWORK ; [monitor flip:self]; }
  269.     else if ( tag == NETWORK ) { [self flipMonitor]; }
  270.     return self;
  271. }
  272.  
  273. - saveDisplayInfo
  274. {
  275.     //    at least one option must be on...
  276.     if ( (displayCpu + displayDisk + displayMemory + displayNetwork) < 1) 
  277.     {
  278.     displayCpu = TRUE;        // Note that this always works, while
  279.     [cpuSwitch setState:1];        // This only works if called from setDisplayCpu?!?
  280.     }
  281.  
  282.     displayInfo[0] = '\0';
  283.     if ( displayCpu )    strcat(displayInfo, "C");
  284.     else        strcat(displayInfo, "c");
  285.     if ( displayDisk )    strcat(displayInfo, "D");
  286.     else        strcat(displayInfo, "d");
  287.     if ( displayMemory ) strcat(displayInfo, "M");
  288.     else        strcat(displayInfo, "m");
  289.     if ( displayNetwork ) strcat(displayInfo, "N");
  290.     else        strcat(displayInfo, "n");
  291.  
  292.     NXWriteDefault([NXApp appName], "DisplayInfo", displayInfo);
  293.     return self;
  294. }
  295.  
  296. - selectedCell
  297. {
  298.     return self;
  299. }
  300.  
  301. - (int)tag
  302. {
  303.     return tag;
  304. }
  305.  
  306. @end
  307.  
  308. /******************************************************************************************************************************
  309.  *    This function is called by a timed entry.                                                                               *
  310.  *    It gets the load data from the system.                                                                                  *
  311.  ******************************************************************************************************************************/
  312. void _step(DPSTimedEntry teNumber,double now,id monitor)
  313. {
  314.     [monitor step];
  315.     return;
  316. }
  317.  
  318. void _flip(DPSTimedEntry teNumber,double now,id self)
  319. {
  320.     [self flipMonitor];
  321.     return;
  322. }
  323.  
  324.  
  325.  
  326.  
  327.